import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login.vue'
import Home from '@/components/Home.vue'
import User from '@/pages/user/index.vue'
import CuisineManagement from '@/pages/cuisine/index.vue'
import TasteManagement from '@/pages/taste/index.vue'
import DishesManagement from '@/pages/dishes/index.vue'
import TableManagement from '@/pages/tables/index.vue'
import OrderManagement from '@/pages/orders/index.vue'
import ReportStatistics from '@/pages/reports/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  
  {
    path: '/',
    meta: {
      titel: '主页'
    },
    redirect: '/reports',
    component: Home,
    children: [
      {
        path: '/user',
        name: 'User',
        component: User
      },
      {
        path: '/cuisine',
        name: 'Cuisine',
        component: CuisineManagement
      },
      {
        path: '/taste',
        name: 'Taste',
        component: TasteManagement
      },
      {
        path: '/dishes',
        name: 'Dishes',
        component: DishesManagement
      },
      {
        path: '/tables',
        name: 'Tables',
        component: TableManagement
      },
      {
        path: '/orders',
        name: 'Orders',
        component: OrderManagement
      },
      {
        path: '/reports',
        name: 'Reports',
        component: ReportStatistics
      },
    ]
  }
]

const router = new VueRouter({
  mode:'hash',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})
export default router
